#include<stdio.h>
#include<stdlib.h>

void _mergesort(int* arr, int left,int right, int* tem)
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) / 2;
	_mergesort(arr, left, mid, tem);
	_mergesort(arr, mid+1, right, tem);
	int begin1 = left;
	int end1 = mid;
	int begin2 = mid + 1;
	int end2 = right;
	int index = left;
	while (begin1 <= end1 && begin2 <= end2)
	{

		if (arr[begin1] < arr[begin2])
		{
			tem[index++] = arr[begin1++];
		}
		else
		{
			tem[index++] = arr[begin2++];
		}
	}
	while (begin1 <= end1)
	{
		tem[index++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
		tem[index++] = arr[begin2++];
	}
	for (int i = left; i <= right; i++)
	{
		arr[i] = tem[i];
	}
}

void mergesort(int* arr, int n)
{
	int* tem = (int*)malloc(n * sizeof(int));
	_mergesort(arr, 0, n - 1, tem);
	free(tem);
	tem = NULL;
}


int main()
{
	int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
	int n = sizeof(arr) / sizeof(arr[0]);
	mergesort(arr, n);
	for (int i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}